package cn.jianyongjie.org.service.impl;

import cn.jianyongjie.basic.exception.CustomException;
import cn.jianyongjie.basic.utils.AjaxResult;
import cn.jianyongjie.basic.utils.BaiduAuditUtils;
import cn.jianyongjie.basic.vo.EcharsShopVo;
import cn.jianyongjie.org.domain.Employee;
import cn.jianyongjie.org.domain.Shop;
import cn.jianyongjie.org.domain.ShopAuditLog;
import cn.jianyongjie.org.mapper.EmployeeMapper;
import cn.jianyongjie.org.mapper.ShopAuditLogMapper;
import cn.jianyongjie.org.mapper.ShopMapper;
import cn.jianyongjie.org.service.IEmployeeService;
import cn.jianyongjie.org.service.IShopService;
import cn.jianyongjie.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import sun.dc.pr.PRError;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jianyongjie
 * @since 2022-05-01
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper eployeeMapper;

    @Autowired
    private IEmployeeService iEmployeeService;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;


    @Autowired
    private JavaMailSender javaMailSender;

    /*
    * @Title: ShopServiceImpl
    * @Description: 店铺入驻
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/9 8:44
    * @Parameters: * @param shop
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @Override
    public AjaxResult settlement(Shop shop) {

        // 判断用户是否填写数据
        if (shop.getName()==null){
            throw new CustomException("店铺名称不能为空");
        }
        if (shop.getTel()==null){
            throw new CustomException("电话不能为空");
        }
        if (shop.getAddress()==null){
            throw new CustomException("地址不能为空");
        }
        if (shop.getAdmin().getUsername()==null){
            throw new CustomException("店长姓名不能为空");
        }
        if (shop.getAdmin().getPhone()==null){
            throw new CustomException("店长电话不能为空");
        }
        if (shop.getAdmin().getEmail()==null){
            throw new CustomException("店长邮件不能为空");
        }
        if (shop.getAdmin().getPassword()==null){
            throw new CustomException("店长密码不能为空");
        }

        // 抽出重复代码
        String name = shop.getName();
        // 判断用户填写的店铺名字是否已经有了
        Shop shopByName = shopMapper.findShopByName(name);
        // 如果等于null，就代表没有储存过该店铺，反之就代表已经有了该店铺了
        if(shopByName!=null){
            throw new CustomException("对不起，该店铺名称已存在");
        }

        // 判断店铺昵称是否违规
        if (!BaiduAuditUtils.TextCensor(name)){
            throw new CustomException("店铺名称违规，请更改");
        }

        // 判断店铺昵称是否违规
        if (!BaiduAuditUtils.TextCensor(shop.getAddress())){
            throw new CustomException("店铺地址违规，请更改");
        }

        // 抽出重复代码
        String tel = shop.getTel();
        // 查询店铺电话是否重复
        Shop shopByTel = shopMapper.findShopByTel(tel);
        // 如果不等于null，就代表店铺电话重复了，返回提示语
        if(shopByTel!=null){
            throw new CustomException("店铺座机号码重复，请重新输入");
        }

        // 抽出重复代码
        String phone = shop.getAdmin().getPhone();
        // 查询员工电话是否重复
        Shop shopByPhone = eployeeMapper.findShopByPhone(phone);
        // 如果不等于null，就代表店铺电话重复了，返回提示语
        if(shopByPhone!=null){
            throw new CustomException("员工电话号码重复，请重新输入");
        }

        // 抽出重复代码
        String email = shop.getAdmin().getEmail();
        // 查询员工邮箱是否重复
        Shop shopByEmail = eployeeMapper.findShopByEmail(email);
        // 如果不等于null，就代表店铺电话重复了，返回提示语
        if(shopByEmail!=null){
            throw new CustomException("员工邮箱重复，请重新输入");
        }


        // 抽出重复代码
        String username = shop.getAdmin().getUsername();
        // 抽出重复代码
        Employee admin = shop.getAdmin();
        // 查询账号是否已经存在了
        Employee empByUsername = eployeeMapper.findEmpByUsername(username);


        // 当不为null的时候就代表已经拥有店铺了，就直接返回提示语
        if (empByUsername!=null&&empByUsername.getShop_id()!=null){
            throw new CustomException("对不起，该店长已经绑定了店铺,请登录");
        }

        // 如果为null就代表没有绑定，就新增一个店长
        if (empByUsername==null){

            // 新增之前先判断店长名称是否违规
            if (!BaiduAuditUtils.TextCensor(username)){
                throw new CustomException("店长名称违规，请更改");
            }

            // 新增店长
            iEmployeeService.save(admin);
            // 在将值赋值给empByUsername
            empByUsername = admin;
        }

        // 因为上面的if判断，已经保证代码执行到了这里肯定会有数据所以只需要将店长id设置给店铺
        shop.setAdminId(empByUsername.getId());
        // 新增店铺
        shopMapper.save(shop);
        // 将店铺id设置给店长
        empByUsername.setShop_id(shop.getId());
        // 最后修改店长信息
        eployeeMapper.update(empByUsername);


//        // 进行判断
//        if (empByUsername!=null&&empByUsername.getShop_id()!=null){
//
//            // 判断店长是否已经有了店铺
//            if(empByUsername.getShop_id()!=null){
//                throw new CustomException("对不起，该店长已经绑定了店铺,请登录");
//            }else {
//
//                // 该店长已经注册了但没有店铺就直接绑定店铺
//                // 将店长的id赋值到店铺AdminId里面
//                shop.setAdminId(empByUsername.getId());
//                // 新增店铺
//                shopMapper.save(shop);
//                // 然后在将新增的店铺id赋值给店长的Shop_id
//                empByUsername.setShop_id(shop.getId());
//                // 修改店长信息
//                eployeeMapper.update(empByUsername);
//
//            }
//
//        }else {
//
//            // 保存店铺
//            shopMapper.save(shop);
//            // 获取店铺新增的id，然后新增到员工表里面
//            shop.getAdmin().setShop_id(shop.getId());
//            // 新增员工表
//            eployeeMapper.save(shop.getAdmin());
//            // 在将员工表的id设置到店铺的员工id里面
//            shop.setAdminId(shop.getAdmin().getId());
//            // 进行修改
//            shopMapper.update(shop);
//
//        }



        return null;
    }

    /*
    * @Title: ShopServiceImpl
    * @Description: 批量新增
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/5 16:22
    * @Parameters: * @param shops
    * @Return void
    */
    @Override
    public void addShops(List<Shop> shops) {
        shopMapper.addShops(shops);
    }


    /*
    * @Title: ShopServiceImpl
    * @Description: 分组查询状态条数
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/5 21:21
    * @Parameters: * @param
    * @Return java.util.List<java.lang.Long>
    */
    @Override
    public List<EcharsShopVo> findShopStatistics() {
        return shopMapper.findShopStatistics();
    }


    /*
    * @Title: ShopServiceImpl
    * @Description: 审核通过业务
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/6 12:40
    * @Parameters: * @param shopAuditLog
    * @Return void
    */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {

        System.out.println(shopAuditLog.getAudit_id());

        Long shop_id = shopAuditLog.getShop_id();
        if (shop_id!=null){
            // 审核通过就先通过id查询数据，再更改状态
            Shop shop = shopMapper.LoadById(shop_id);
            // 更改状态
            shop.setState(2);
            // 进行修改状态
            shopMapper.update(shop);
            // 发送激活请求
            SimpleMailMessage simp = new SimpleMailMessage();
            // 设置发送人
            simp.setFrom("2102768810@qq.com");
            // 设置主题
            simp.setSubject("激活信息");
            // 设置内容
            simp.setText("您的店铺已激活,这是激活链接:http://127.0.0.1:8081/#/login");
            // 设置收件人
            simp.setTo("2102768810@qq.com");

            // 发送信息
            javaMailSender.send(simp);
        }

    }


    /*
    * @Title: ShopServiceImpl
    * @Description: 审核驳回业务
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/6 12:40
    * @Parameters: * @param shopAuditLog
    * @Return void
    */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {

        try {

            // 抽出店铺id
            Long shop_id = shopAuditLog.getShop_id();
            // 只有不等于null才发送信息
            if (shop_id!=null){
                // 审核通过就先通过id查询数据，再更改状态
                Shop shop = shopMapper.LoadById(shop_id);
                // 更改驳回状态
                shop.setState(-1);
                // 进行修改将状态更改
                shopMapper.update(shop);

                // 设置驳回日志时间
                shopAuditLog.setAudit_time(new Date());
                // 进行储存
                shopAuditLogMapper.save(shopAuditLog);

                // 发送修改邮件
                SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
                // 设置发送人
                simpleMailMessage.setFrom("2102768810@qq.com");
                // 设置主题
                simpleMailMessage.setSubject("审核驳回信息");
                // 设置文本
                simpleMailMessage.setText("您填写的信息,存在有问题请进行修改,修改链接为http://127.0.0.1:8081/#/register2?id="+shop_id+"");

                // 设置收件人
                simpleMailMessage.setTo("2102768810@qq.com");

                // 发送信息
                javaMailSender.send(simpleMailMessage);

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
